#include "main.h"

SYS_TIME_T          sys_time_t;
SYS_TIME_T          sys_time_t_bak;

extern LCD1602_T    lcd1602_t;
extern IR_T         ir_t;
extern DS18B20_T    ds18b20_t;
extern UART_T       uart_t;
extern KEYBOARD_T   keyboard_t;
extern SYSTEM_UI_T  system_ui_t;

const INT8U *delayMs[] = {
                           &uart_t.delay_ms,
                           &ds18b20_t.delay_ms,
                           &ir_t.delay_ms,
                           &keyboard_t.delay_ms,
                           &lcd1602_t.delay_ms,
                           &system_ui_t.delay_ms
                         };
/*****************************************************************************
 �� �� ��  : system_1ms_init
 ��������  : ��ʼ��ϵͳ1ms��ʱ��
 �������  : ��      
 �������  : ��
 �� �� ֵ  : ��
 �޸���ʷ      :
  1.��    ��   : 2019��03��03��
    ��    ��   : ΢Ц����
    �޸�����   : ����
*****************************************************************************/
void system_1ms_init(void)
{
    SET_TIMER0_TIMER();
    SET_TIMER0_SIXTEEN_MODE();
    
    TH0 = (65536 - 1000)/256;
    TL0 = (65536 - 1000)%256;
    
    INTERRUPT_TIMER0_ENABLE();
    TIMER0_START();
    
    sys_time_t.state = CLOCK_RUNNING;
}

/*****************************************************************************
 �� �� ��  : system_1ms_handler
 ��������  : ϵͳ1ms��ʱ��
 �������  : ��      
 �������  : ��
 �� �� ֵ  : ��
 �޸���ʷ      :
  1.��    ��   : 2019��03��03��
    ��    ��   : ΢Ц����
    �޸�����   : ����
*****************************************************************************/
void  system_1ms_handler(void)
{
    INT8U i;
    
    for(i = 0; i < sizeof(delayMs)/4; i ++)
    {
        if(delayMs[i] > 0)
        {
           delayMs[i] --;
        }
    }

    sys_time_t.msec++;
    sys_time_t.timer_msec++;
    if(sys_time_t.msec >= 1000)
    {
        sys_time_t.msec = 0;
        sys_time_t.sec++;
        if(sys_time_t.sec >= 60)
        {
            sys_time_t.sec = 0;
            sys_time_t.min++;
            if(sys_time_t.min >= 60)
            {
                sys_time_t.min = 0;
                sys_time_t.hour++;
                if(sys_time_t.hour >= 24)
                {
                    sys_time_t.month_day ++;
                    sys_time_t.hour = 0;
                }
            }
        }
    }
    
    if(CLOCK_RUNNING == sys_time_t.state && (system_ui_t.state != ALARM_TIME && system_ui_t.state != ALARM_TIME_SETING))
    {
        if(sys_time_t.timer_msec >= 1000)
        {
               sys_time_t.timer_msec = 0;
                 if(system_ui_t.delay_s)
           {
              system_ui_t.delay_s --;
           }
            }
    }

    else
    {
        if(sys_time_t.timer_msec >= 70)
        {
            sys_time_t.timer_msec = 0;
            if(system_ui_t.delay_s)
            {
                system_ui_t.delay_s --;
            }
        }
    }
}
void timer0_interrupt()    interrupt 1
{
    TH0 = (65536 - 1000)/256 + TH0;
    TL0 = (65536 - 1000)%256 + TL0;
    system_1ms_handler();
}


/*****************************************************************************
 �� �� ��  :delay_10us
 ��������  : ��ȷ��ʱ10us
 �������  : ��      
 �������  : ��
 �� �� ֵ  : ��
 �޸���ʷ      :
  1.��    ��   : 2019��03��03��
    ��    ��   : ΢Ц����
    �޸�����   : ����
*****************************************************************************/
void delay_10us(void)
{
    INT8U a,b;
    for(b = 1;b > 0;b --)
        for(a = 2;a > 0;a --);
}

/*****************************************************************************
 �� �� ��  : delayms
 ��������  : ��ȷ��ʱms
 �������  : count      
 �������  : ��
 �� �� ֵ  : ��
 �޸���ʷ      :
  1.��    ��   : 2019��03��03��
    ��    ��   : ΢Ц����
    �޸�����   : ����
*****************************************************************************/
void delay_ms(INT8U count)  
{
    INT8U i,j;
    for(i = 0;i < count;i ++)
    {
        for(j = 0;j < 100;j ++);
    }
}

/*****************************************************************************
 �� �� ��  :delay_100us
 ��������  : ��ȷ��ʱ100us
 �������  : ��      
 �������  : ��
 �� �� ֵ  : ��
 �޸���ʷ      :
  1.��    ��   : 2019��03��03��
    ��    ��   : ΢Ц����
    �޸�����   : ����
*****************************************************************************/
void delay100us(void)   
{
    unsigned char a,b;
    for(b=19;b>0;b--)
        for(a=1;a>0;a--);
}

void get_system_time(INT8U *_time_buffer, SYS_TIME_T *_sys_time_t, INT8U _flag)
{
    if(_flag)
    {
        _time_buffer[0]  = _sys_time_t->year / 1000       + 0x30;
        _time_buffer[1]  = _sys_time_t->year % 1000 / 100 + 0x30;
        _time_buffer[2]  = _sys_time_t->year % 100  / 10  + 0x30;
        _time_buffer[3]  = _sys_time_t->year % 10         + 0x30;
        _time_buffer[4]  = '/';
        _time_buffer[5]  = _sys_time_t->month / 10 + 0x30;
        _time_buffer[6]  = _sys_time_t->month %10  + 0x30;
        _time_buffer[7]  = '/';
        _time_buffer[8]  = _sys_time_t->month_day / 10 + 0x30;
        _time_buffer[9]  = _sys_time_t->month_day %10  + 0x30;
    }
    else
    {
        _time_buffer[0]  = _sys_time_t->hour / 10 + 0x30;
        _time_buffer[1]  = _sys_time_t->hour % 10 + 0x30;
        _time_buffer[2]  = ':';
        _time_buffer[3]  = _sys_time_t->min / 10  + 0x30;
        _time_buffer[4]  = _sys_time_t->min %10   + 0x30;
        _time_buffer[5]  = ':';
        _time_buffer[6]  = _sys_time_t->sec / 10  + 0x30;
        _time_buffer[7]  = _sys_time_t->sec %10   + 0x30;
    }
    return;
}

